import java.util.HashMap;
import java.util.Stack;

/**
 * @Auther: wangliuyang
 * @Date: 2021/4/24 17:06
 * @Description: 20. 有效的括号
 */
public class Solution_20 {
    public boolean valid(String s) {
        if (s == null || s.length() < 2) {
            return  false;
        }
        char[] chars = s.toCharArray();
        HashMap<Character, Character> map = new HashMap();
        map.put('(',')');
        map.put('[',']');
        map.put('{','}');
        Stack<Character> stack = new Stack();
        for (char c : chars) {
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            }else {
                if (stack.isEmpty() || !map.get(stack.peek()).equals(c)) {
                    return false;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
